package edu.cmu.lti.ritesdk.sample;

import edu.cmu.lti.ritesdk.AbstractRiteSystem;
import edu.cmu.lti.ritesdk.AnalyzedTextPair;
import edu.cmu.lti.ritesdk.TextPair;

/**
 * Decides on a label based on the portion of t2 contained in t1 in character-level.
 * 
 * @author Hideki Shima
 *
 */
public class CharOverlapBasedSystem extends AbstractRiteSystem {

  @Override
  public AnalyzedTextPair run(TextPair t) {
    System.out.println("---- "+t.getId()+" "+t.getGoldStandardLabel()+" ----\n"+t.getT1()+"\n"+t.getT2());

    char[] t1Chars = t.getT1().toCharArray();
    char[] t2Chars = t.getT2().toCharArray();

    int numContained = 0; 
    for ( int j=0; j<t2Chars.length; j++ ) {
      for ( int i=0; i<t1Chars.length; i++ ) {
        char c1 = t1Chars[i];
        char c2 = t2Chars[j];
        if (c1 == c2) { 
          numContained++;
          // for clip-counting (cf. BLEU)
          t1Chars[i] = Character.MAX_VALUE;
          break;
        }
      }
    }
    double coverage = (double)numContained / (double) t2Chars.length;
    System.out.println("Character Overlap = "+coverage);
    AnalyzedTextPair result;
    if ( coverage > 0.6d ) {
      result = new AnalyzedTextPair( t, "Y", coverage );
    } else {
      result = new AnalyzedTextPair( t, "N", 1-coverage );
    }    
    return result;
  }

}
